home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.5)
-
-
- try:
- import ctypes
- import ctypes.util as ctypes
- except ImportError:
- raise ImportError('ctypes library is needed')
-
-
- class AspellError(Exception):
- pass
-
-
- class AspellConfigError(AspellError):
- pass
-
-
- class AspellSpellerError(AspellError):
- pass
-
-
- class Aspell(object):
- VERSION = 5
- if VERSION == 5:
- LIBNAME = 'aspell\\bin\\aspell-15'
- elif VERSION == 6:
- LIBNAME = 'aspell\\bin\\aspell'
-
-
- def __init__(self, configkeys = None, libname = None, **kwds):
- if libname is None:
- libname = ctypes.util.find_library(self.LIBNAME)
-
- self._Aspell__lib = ctypes.CDLL(libname)
- self.libname = libname
- config = self._Aspell__lib.new_aspell_config()
- if config == None:
- raise AspellError("Can't create aspell config object")
-
- if configkeys:
- if hasattr(configkeys, 'items'):
- configkeys = configkeys.items()
-
- if len(configkeys) == 2 and type(configkeys[0]) is str and type(configkeys[1]) is str:
- configkeys = [
- configkeys]
-
- configkeys = dict(configkeys)
- else:
- configkeys = { }
- configkeys.update(kwds)
- configkeys = configkeys.items()
- for key, value in configkeys:
- if not self._Aspell__lib.aspell_config_replace(config, key, value):
- raise self._aspell_config_error(config)
- continue
-
- possible_error = self._Aspell__lib.new_aspell_speller(config)
- self._Aspell__lib.delete_aspell_config(config)
- errno = self._Aspell__lib.aspell_error_number(possible_error)
- if errno != 0:
- errmsg = ctypes.string_at(self._Aspell__lib.aspell_error_message(possible_error))
- self._Aspell__lib.delete_aspell_can_have_error(possible_error)
- raise AspellError(errno, errmsg)
-
- self._Aspell__speller = self._Aspell__lib.to_aspell_speller(possible_error)
-
-
- def check(self, word):
- if type(word) is str:
- return bool(self._Aspell__lib.aspell_speller_check(self._Aspell__speller, word, len(word)))
- else:
- raise TypeError('String expeced')
-
-
- def suggest(self, word):
- if type(word) is str:
- return self._aspellwordlist(self._Aspell__lib.aspell_speller_suggest(self._Aspell__speller, word, len(word)))
- else:
- raise TypeError('String expeced')
-
-
- def personal_dict(self, word = None):
- if word is not None:
- self._Aspell__lib.aspell_speller_add_to_personal(self._Aspell__speller, word, len(word))
- self._aspell_check_error()
- else:
- return self._aspellwordlist(self._Aspell__lib.aspell_speller_personal_word_list(self._Aspell__speller))
-
-
- def session_dict(self, word = None, clear = False):
- if clear:
- self._Aspell__lib.aspell_speller_clear_session(self._Aspell__speller)
- self._aspell_check_error()
- return None
-
- if word is not None:
- self._Aspell__lib.aspell_speller_add_to_session(self._Aspell__speller, word, len(word))
- self._aspell_check_error()
- else:
- return self._aspellwordlist(self._Aspell__lib.aspell_speller_session_word_list(self._Aspell__speller))
-
-
- def add_replacement_pair(self, misspelled, correct):
- self._Aspell__lib.aspell_speller_store_replacement(self._Aspell__speller, misspelled, len(misspelled), correct, len(correct))
- self._aspell_check_error()
-
-
- def save_all(self):
- self._Aspell__lib.aspell_speller_save_all_word_lists(self._Aspell__speller)
- self._aspell_check_error()
-
-
- def configkeys(self):
- config = self._Aspell__lib.aspell_speller_config(self._Aspell__speller)
- if config is None:
- raise AspellConfigError("Can't get speller's config")
-
- keys_enum = self._Aspell__lib.aspell_config_possible_elements(config, 1)
- if keys_enum is None:
- raise AspellError("Can't get list of config keys")
-
-
- class KeyInfo(ctypes.Structure):
- _fields_ = [
- ('name', ctypes.c_char_p),
- ('type', ctypes.c_int),
- ('default', ctypes.c_char_p),
- ('desc', ctypes.c_char_p),
- ('flags', ctypes.c_int),
- ('other_data', ctypes.c_int)]
-
- key_next = self._Aspell__lib.aspell_key_info_enumeration_next
- key_next.restype = ctypes.POINTER(KeyInfo)
- list = []
- while True:
- key_info = key_next(keys_enum)
- if not key_info:
- break
- else:
- key_info = key_info.contents
- if key_info.type == 0:
- list.append((key_info.name, key_info.default, key_info.desc))
- continue
- if key_info.type == 1:
- list.append((key_info.name, int(key_info.default), key_info.desc))
- continue
- if key_info.type == 2:
- if key_info.default.lower() == 'true':
- list.append((key_info.name, True, key_info.desc))
- else:
- list.append((key_info.name, False, key_info.desc))
- key_info.default.lower() == 'true'
- if key_info.type == 3:
- list.append((key_info.name, key_info.default.split(), key_info.desc))
- continue
- self._Aspell__lib.delete_aspell_key_info_enumeration(keys_enum)
- return list
-
-
- def reset_cache(self):
- return self._reload_lib()
-
-
- def close(self):
- self._Aspell__lib.delete_aspell_speller(self._Aspell__speller)
-
-
- def _reload_lib(self):
- _LoadLibrary = LoadLibrary
- _FreeLibrary = FreeLibrary
- import _ctypes
- if self._Aspell__lib._handle != 0:
- _FreeLibrary(self._Aspell__lib._handle)
- del self._Aspell__lib
- self._Aspell__lib = ctypes.CDLL(self.libname)
-
-
-
- def _aspellwordlist(self, wordlist_id):
- elements = self._Aspell__lib.aspell_word_list_elements(wordlist_id)
- list = []
- while True:
- wordptr = self._Aspell__lib.aspell_string_enumeration_next(elements)
- if not wordptr:
- break
- continue
- word = ctypes.c_char_p(wordptr)
- list.append(word.value)
- self._Aspell__lib.delete_aspell_string_enumeration(elements)
- return list
-
-
- def _aspell_config_error(self, config):
- exc = AspellConfigError(ctypes.c_char_p(self._Aspell__lib.aspell_config_error_message(config)).value)
- self._Aspell__lib.delete_aspell_config(config)
- raise exc
-
-
- def _aspell_check_error(self):
- if self._Aspell__lib.aspell_speller_error(self._Aspell__speller) != 0:
- msg = self._Aspell__lib.aspell_speller_error_message(self._Aspell__speller)
- raise AspellSpellerError(ctypes.string_at(msg))
-
-
-
- _test_basic = '>>> a = Aspell((\'lang\', \'en\'))\n>>> a.check("when")\nTrue\n>>> set(a.suggest("wehn")) == set([\'when\', \'wen\', \'wean\', \'ween\', \'Wehr\', \'whens\', \'hen\'])\nTrue\n>>> a.add_replacement_pair("wehn", "ween")\n>>> set(a.suggest("wehn")) == set([\'ween\', \'when\', \'wen\', \'wean\', \'Wehr\', \'whens\', \'hen\'])\nTrue\n>>> a.session_dict() == []\nTrue\n>>> a.check("pyaspell")\nFalse\n>>> a.session_dict("pyaspell")\n>>> a.session_dict() == [\'pyaspell\']\nTrue\n>>> a.check("pyaspell")\nTrue\n>>> a.session_dict(clear=True)\n>>> a.session_dict()\n[]\n>>> a.close()'
- _test_locales = ">>> # Locales\n>>> a1 = Aspell(('lang', 'en-us'))\n>>> a2 = Aspell(('lang', 'en-gb'))\n>>> (a1.check('localize'), a2.check('localize')) == (True, False)\nTrue\n>>> (a1.check('localize'), a2.check('localize')) == (True, False)\nTrue\n>>> (a1.check('localise'), a2.check('localise')) == (False, True)\nTrue\n>>> (a1.suggest('localise')[0], a2.suggest('localize')[0]) == ('localize', 'localise')\nTrue\n>>> a1.close()\n>>> a2.close()"
- _test_case = ">>> # Case sensitivity\n>>> a1 = Aspell(('ignore-case',''))\n>>> a2 = Aspell(('dont-ignore-case', ''))\n>>> (a1.check('steve'), a2.check('steve')) == (True, False)\nTrue\n>>> (a1.check('Steve'), a2.check('Steve')) == (True, True)\nTrue\n>>> a1.close()\n>>> a2.close()"
- __test__ = dict(basic = _test_basic, locales = _test_locales, case = _test_case)
-
- def _test():
- import doctest
- doctest.testmod(verbose = True)
-
- if __name__ == '__main__':
- Aspell(('lang', 'fake'))
-
-